package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import util.DomainUtils;

public class ActionDispatcherValidationForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	private String selected_contact = null;
	private String wanted_action = null;
	private String[] contactgroups = null;
	private String keywords = null;

	
	
	public String getSelected_contact() {
		return selected_contact;
	}

	public void setSelected_contact(String selected_contact) {
		this.selected_contact = selected_contact;
	}

	public String getWanted_action() {
		return wanted_action;
	}

	public void setWanted_action(String wanted_action) {
		this.wanted_action = wanted_action;
	}

	public String[] getContactgroups() {
		return contactgroups;
	}

	public void setContactgroups(String[] contactgroups) {
		this.contactgroups = contactgroups;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = DomainUtils.trim(keywords);
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.selected_contact = null;
		this.wanted_action = null;
		this.contactgroups = null;
		this.keywords = null;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);

		// Checking session
		if (session == null
				|| session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"session.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}
		
		if(getSelected_contact() == null || getSelected_contact().isEmpty()) {
			errors.add("selected_contact", new ActionMessage(
					"actiondispatcher.selected_contact.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}
		
		if(getWanted_action() == null){
			errors.add("wanted_action", new ActionMessage(
					"actiondispatcher.wanted_action.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}
		// Checking condition for add to group
		if (getWanted_action().compareTo("add_to_contactgroup") == 0) {
			if (getContactgroups() == null || getContactgroups().length == 0) {
				errors.add("wanted_action", new ActionMessage(
						"actiondispatcher.wanted_action.needgroup.error"));

				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}

		// Checking condition for remove from group
		if (getWanted_action().compareTo("remove_from_contactgroup") == 0) {
			if (getContactgroups() == null || getContactgroups().length == 0) {
				errors.add("wanted_action", new ActionMessage(
						"actiondispatcher.wanted_action.needgroup.error"));
				
				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}
		
		return errors;
	}
}
